package com.zy.cat.controller.game.v1;

import java.util.concurrent.Callable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zy.cat.common.annotation.CheckDevPermission;
import com.zy.cat.common.annotation.CheckingMD5;
import com.zy.cat.common.utils.TJKUtils;
import com.zy.cat.dao.Dao;
import com.zy.cat.entity.game.BoxDrawCountHistory;
import com.zy.cat.entity.game.repository.BoxDrawCountHistoryRepository;
import com.zy.cat.entity.game.resp.BoxInfoResp;
import com.zy.cat.entity.resp.RSPE;
import com.zy.cat.service.CacheService;
import com.zy.cat.service.CatGameService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "9catGameBox", tags = { "小游戏-开宝箱-开放API" }, description = "小游戏-开宝箱-开放接口")
@RestController

@RequestMapping("/v1/game/box")
public class CatGameBoxController {

	@Autowired
	CacheService cacheService;

	@Autowired
	CatGameService catGameService;
	
	@Autowired
	BoxDrawCountHistoryRepository countHistoryRepository;
	
	@Autowired
	Dao<BoxInfoResp> boxInfoRespDao;
 

	@PostMapping("/info")
	@ApiOperation(value = "开宝箱初始化信息", notes = "每次进入请求，返回游戏进度等信息", consumes = "application/x-www-form-urlencoded", position = 1, response = BoxInfoResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_rtype_utoken_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_pid_rtype_utoken_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getBoxInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<BoxInfoResp>> result = new Callable<RSPE<BoxInfoResp>>() {
			@Override
			public RSPE<BoxInfoResp> call() throws Exception {
				BoxInfoResp result = catGameService.getBoxInfo(rtype, pid);
				if (result == null) {
					return new RSPE<BoxInfoResp>(-3, "获取信息失败！请稍后重试！");
				}
				return new RSPE<BoxInfoResp>(result);
			}
		};
		return result;
	}
	
	@PostMapping("/addkey")
	@ApiOperation(value = "领取开宝箱钥匙", notes = "领取开宝箱钥匙 ！", consumes = "application/x-www-form-urlencoded", position = 3, response = RSPE.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "type", value = "钥匙获取类型（0.水果机、1.神奇便利店、2.大转盘、3.刮刮卡）", example = "1", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_utoken_type_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_pid_utoken_type_rtype_uuid", MD5 = "token",checkSingle = true)
	@CheckDevPermission
	public Object addBoxKey(Integer rtype,Integer type,  String pid , String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (type==null||type<0||type>3) {
			return new RSPE<Object>(-3, "钥匙获取类型参数错误！");
		}
		
		/* 1.判断此用户该钥匙类型今日是否已经领取 */
		BoxDrawCountHistory drawCountHistory = countHistoryRepository.findByPidAndDtypeAndCtime(pid, type,
				TJKUtils.DateTime(TJKUtils.YYYY_MM_DD));
		if (drawCountHistory!=null) {
			return new RSPE<Object>(-4, "该游戏钥匙已领取！");
		}
		Callable<RSPE<Boolean>> result = new Callable<RSPE<Boolean>>() {
			@Override
			public RSPE<Boolean> call() throws Exception {
				if(catGameService.addBoxKey(rtype,pid,type)) {
					return new RSPE<Boolean>(1,"领取成功！");
				}
				return new RSPE<Boolean>(-1,"领取失败！");
			}
		};
		return result;
	}
	
	@PostMapping("/draw")
	@ApiOperation(value = "开宝箱", notes = "收集4个钥匙可开一次宝箱", consumes = "application/x-www-form-urlencoded", position = 2, response = BoxInfoResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_utoken_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_pid_utoken_rtype_uuid", MD5 = "token",checkSingle = true)
	@CheckDevPermission
	public Object getBoxCardDraw(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		/* 1.判断钥匙数是否集齐 */
		BoxInfoResp boxInfoResp = boxInfoRespDao.selectOne(new BoxInfoResp(pid), "getBoxInfo");
		if (boxInfoResp.getDcount()<4) {
			return new RSPE<Object>(-3, "未收集齐开宝箱钥匙！");
		}
		Callable<RSPE<BoxInfoResp>> result = new Callable<RSPE<BoxInfoResp>>() {
			@Override
			public RSPE<BoxInfoResp> call() throws Exception {
				return new RSPE<BoxInfoResp>(catGameService.getBoxCardDraw(rtype, pid));
			}
		};
		return result;
	}
	
	@PostMapping("/popup/status")
	@ApiOperation(value = "游戏退出时弹窗状态更新", notes = "游戏退出时弹窗状态更新", consumes = "application/x-www-form-urlencoded", position = 3, response = RSPE.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "pindex", value = "弹窗位置(1.刮刮卡、2.神奇便利店)", example = "0", dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_utoken_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_pid_utoken_rtype_uuid", MD5 = "token",checkSingle = true)
	@CheckDevPermission
	public Object updatePopupStatus(Integer rtype, String pid,Integer pindex, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (pindex == null || (pindex != 1 && pindex != 2)) {
			return new RSPE<Object>(-3, "弹窗位置参数错误！");
		}
		Callable<RSPE<Boolean>> result = new Callable<RSPE<Boolean>>() {
			@Override
			public RSPE<Boolean> call() throws Exception {
				Boolean status = catGameService.updatePopupStatus(rtype, pid,pindex);
				return new RSPE<Boolean>(status);
			}
		};
		return result;
	}
}
